package com.youren.health.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.youren.health.constant.MessageConstant;
import com.youren.health.dao.CheckItemDao;
import com.youren.health.entity.PageResult;
import com.youren.health.entity.QueryPageBean;
import com.youren.health.exception.HealthException;
import com.youren.health.pojo.CheckItem;
import com.youren.health.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

/**
 * @program: health_parent
 * @author: You_ren_a
 * @create: 2020-11-21 17:09
 **/
@Service(interfaceClass = CheckItemService.class)
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemDao checkItemDao;
    @Override
    public List<CheckItem> findAll() {
        return checkItemDao.findAll();
    }

    @Override
    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
//        if (!StringUtils.isEmpty(queryPageBean.getQueryString())){
//            queryString="%"+queryString+"%";
//        }
        Page<CheckItem> page = checkItemDao.findByPage(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    @Transactional
    public void deleteById(Integer id) throws HealthException{
        Integer countByCheckItemId = checkItemDao.findCountByCheckItemId(id);
        if (countByCheckItemId > 0) {
            throw new HealthException(MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        checkItemDao.deleteById(id);
    }

    @Override
    public CheckItem findById(Integer id) {
        CheckItem checkItem = checkItemDao.findById(id);
        return checkItem;
    }

    @Override
    public void update(CheckItem checkItem) {
        checkItemDao.update(checkItem);
    }
}
